﻿//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , lusens 
//------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Utility.Data
{
    /// <summary>
    /// SQLServer数据库操作类
    /// 本类主要包括SQLServer数据库的基本操作
    /// 
    /// 修改纪录
    /// 
    ///     2010.10.22 版本：1.1 lusens 新增了用户传入sql语句列表的事务操作
    ///		2010.09.05 版本：1.0 lusens 创建。
    /// 
    /// 版本：1.1
    /// 
    /// <author>
    ///		<name>lusens</name>
    ///		<date>2010.09.05</date>
    ///		<EMail>lusens@foxmail.com</EMail>
    /// </author> 
    /// </summary>
    public abstract class SQLServerHepler
    {
        private  static readonly string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;


        #region 执行SQL，返回被操作的行数,public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)

        /// <summary>
        /// 执行SQL语句，返回被操作的行数
        /// 使用using语句进行conn对象的释放
        /// </summary>
        /// <param name="connectionString">连接字符</param>
        /// <param name="cmdType">Command类型，SQL语句或存储过程</param>
        /// <param name="cmdText">SQL语句或存储过程名称</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns>返回被操作行数</returns>
        public static int ExecuteNonQuery( CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return val;
            }
        }
        #endregion


      
        #region 执行SQL，返回SqlDataReader,public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        /// <summary>
        /// 执行SQL，返回SqlDataReader
        /// 返回一个连接，所以不能进行conn释放，在外界代码中使用完DataReader后，注意需要释放reader对象
        /// 当返回连接对象报错时，这里进行数据库连接的关闭，保证数据库连接使用完成后保持关闭
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="cmdType">command命令类型，SQL语句或存储过程</param>
        /// <param name="cmdText">SQL语句或存储过程名称</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(connectionString);

            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return rdr;
            }
            catch (Exception e)
            {
                conn.Close();
                throw e;
            }
        }
        #endregion

  
        #region 执行SQL，返回DataTable， public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        /// <summary>
        /// 执行SQL，返回DataTable
        /// 使用using语句进行conn对象的释放
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="cmdType">command命令类型，SQL语句或存储过程</param>
        /// <param name="cmdText">SQL语句或存储过程名称</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public static DataTable ExecuteDataTable( CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                }
            }

            return dt;
        }
        #endregion


        #region 返回第一行第一列，public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        /// <summary>
        /// 返回第一行第一列
        /// 使用using语句进行conn对象的释放
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="cmdType">Command命令类型，SQL语句还是存储过程</param>
        /// <param name="cmdText">SQL语句或存储过程名称</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns></returns>
        public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return val;
            }
        }
        #endregion


        #region 构造SQLCommand,private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        /// <summary>
        /// 构造SQLCommand
        /// </summary>
        /// <param name="cmd">Command对象</param>
        /// <param name="conn">SqlConnection数据库连接对象</param>
        /// <param name="trans">SQL事务</param>
        /// <param name="cmdType">Command命令类型</param>
        /// <param name="cmdText">SQL语句或存储过程名称</param>
        /// <param name="cmdParms">参数数组</param>
        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }
        #endregion
    }
}